package com.bocai.dahuatech.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.AddressListBean;
import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.utils.SharePrefencesUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddressEditActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.et_addr_edit_name)
    EditText etName;
    @Bind(R.id.et_addr_edit_phone)
    EditText etPhone;
    @Bind(R.id.et_addr_edit_postcode)
    EditText etPostcode;
    @Bind(R.id.et_addr_edit_road)
    EditText etRoad;
    @Bind(R.id.tv_addr_edit_city)
    TextView tvCity;
    @Bind(R.id.ll_addr_edit_city)
    LinearLayout llCity;
    @Bind(R.id.btn_addr_edit_commit)
    Button btnCommit;
    String []cityIds;
    String []citys;

    int provinceId,cityId,areaId;

    private AddressListBean.ContentEntity data;
    @Override
    protected int contentView() {
        return R.layout.activity_edit_address;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.addrdetail);
        data= (AddressListBean.ContentEntity) getIntent().getSerializableExtra("data");
        provinceId=data.getFProvinceID();
        cityId=data.getFCityID();
        areaId=data.getFAreaID();
        setData();
        initEvent();
    }

    private void setData() {
        etName.setText(data.getFConsignee());
        etPhone.setText(data.getFMobilePhone());
        etPostcode.setText(data.getFZipCode());
        etRoad.setText(data.getFAddress());
        tvCity.setText(data.getPCAName());
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        llCity.setOnClickListener(this);
        btnCommit.setOnClickListener(this);
    }


    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if(event.msg.equals("citychoseover")){
            tvCity.setText(event.city);
            citys=event.city.split("-");
            cityIds=event.cityId.split("-");
            provinceId=Integer.parseInt(cityIds[0]);
            cityId=Integer.parseInt(cityIds[1]);
            areaId=Integer.parseInt(cityIds[2]);
        }

    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ll_addr_edit_city:
                //选择省市区
                Intent intent=new Intent(this,ProviceListActivity.class);
                intent.putExtra("add",100);
                String []citys=data.getPCAName().split("_");
                intent.putExtra("provice",citys[0]);
                intent.putExtra("proviceId",data.getFProvinceID());
                startActivity(intent);
                break;
            case R.id.btn_addr_edit_commit:
                //保存
                commit();
                break;
        }
    }

    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            hideLoading();
            showToast(R.string.settingsuccese);
            Log.e("info",baseBean.getReturnInfo());
            AddressEditActivity.this.finish();
        }
    };
    private void commit() {
        if(TextUtils.isEmpty(etName.getText().toString()))
        {
            showToast(R.string.consigneenamenotnull);
        }else if (TextUtils.isEmpty(etPhone.getText().toString()))
        {
            showToast(R.string.phonenotnull);
        }
        else if (etPhone.getText().toString().length()!=11)
        {
            showToast(R.string.phonefalse);
        }
        else if (TextUtils.isEmpty(etPostcode.getText().toString()))
        {
            showToast(R.string.emailnotnull);
        }
        else if (etPostcode.getText().toString().length()!=6)
        {
            showToast(R.string.emailfalse);
        }
        else if (TextUtils.isEmpty(tvCity.getText().toString()))
        {
            showToast(R.string.citynolchoose);
        }else if (TextUtils.isEmpty(etRoad.getText().toString()))
        {
            showToast(R.string.addressnotnull);
        }else
        {
            showLoading();
            getTestApi().editAddr(etName.getText().toString(),provinceId,cityId,areaId,data.getPCAName(),data.getFAddress(),data.getFZipCode(),
                    etPhone.getText().toString(), SharePrefencesUtil.getCustomerCode(this),1)
                    .compose(this.<BaseBean>bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        }
    }
}
